package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.ClassDao;
import com.aaa.entity.ClassEntity;
import com.aaa.entity.Pager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ClassDaoImpl extends BaseDao implements ClassDao {
    @Override
    public List<Map> query_class() {
        String sql="select * from class where del_flag=0";
        List<Map> list=executeQuery(sql);
        return list;
    }

    @Override
    public Pager query(int curPage, int pageSize, Map condation) {
        String sql="select course_name,classid,courseid,classname,class.del_flag,progressing,limitnum,acnumber,opentime,endtime,classroom,class.teacher,remark,dept_name from class,course where 1=1 and class.del_flag=0 and class.courseid=course.course_id ";
        StringBuffer buffer=new StringBuffer(sql);
        List params=new ArrayList();
        if (condation!=null)
        {
            if (condation.containsKey("className")&&condation.get("className").toString().length()>0)
            {
                buffer.append("and classname like ?");
                params.add("%"+condation.get("className")+"%");
            }
            if (condation.containsKey("classState")&&condation.get("classState").toString().length()>0)
            {
                buffer.append("and progressing = ?");
                params.add(condation.get("classState"));
            }
        }
        sql="select count(*) c from ("+buffer+") user";
        Map map=this.executeQuery(sql,params.toArray()).get(0);
        Long  count= (Long) map.get("c");

        buffer.append("limit ?,?");
        int startindex=(curPage-1)*pageSize;
        params.add(startindex);
        params.add(pageSize);
       // System.out.println(buffer.toString());
        List list=this.executeQuery(buffer.toString(),params.toArray());
        Pager pager=new Pager(curPage,pageSize,list,count.intValue());
        return pager;
    }
    //查询可用教室
    @Override
    public List<Map> query_class_room() {

        String sql="select * from class_room where del_flag=0 and is_use=0";
        return executeQuery(sql);
    }



    //查询教室带分页的
    @Override
    public Pager query_class_room(int curPage, int pageSize) {
        Long count= (Long)executeQuery("select count(*) c from class_room where del_flag=0").get(0).get("c");
        String sql="select * from class_room where del_flag=0 limit ?,?";
        int startindex=(curPage-1)*pageSize;
        List<Map> map= executeQuery(sql,startindex,pageSize);
        Pager pager=new Pager(curPage,pageSize,map,new Long(count).intValue());
        return pager;
    }

    @Override
    public int add_class_room() {
        return 0;
    }

    @Override
    public int update_class_room(String classRoom,String room_name) {
        String sql="update class_room set room_name=? where room_name=?";
        int result=executeUpdate(sql,classRoom,room_name);
        return result;
    }


    @Override
    public int add_class_up(List list) {
        String sql="insert into class(courseid,classname,limitnum,opentime,endtime,classroom,teacher,dept_name,del_flag,progressing,acnumber)values(?,?,?,?,?,?,?,?,?,?,?)";
        return executeUpdate(sql,list.get(0),list.get(1),list.get(2),list.get(3),list.get(4),list.get(5),list.get(6),list.get(7),0,0,0);
    }

    @Override
    public List<Map> count_acnumber() {
        String sql="select count(*) acnumber,class.classid from class,student where class.classid=student.classid GROUP BY class.classid";
        return executeQuery(sql);

    }

    @Override
    public int update_acnumber(int classid,int acnumber) {
        String sql="update class set acnumber=? where classid=?";
        return executeUpdate(sql,acnumber,classid);
    }

    @Override
    public List<Map> query_without_class() {
        String sql="select * from student where stutate=1";
        return executeQuery(sql);
    }

    @Override
    public List<Map> query_class_one(int id) {
        String sql="select * from class where classid=?";
        return executeQuery(sql,id);
    }

    @Override
    public int update_student_classid(int id,int classid) {
        String sql="update student set classid="+classid+",stutate=0 where stuid=?";
        return executeUpdate(sql,id);
    }

    @Override
    public int update_class_progressing(int classid,int progressing,int is_use) {
        String sql="update class set progressing=? where classid="+classid;
        String sql2= "update class,class_room set progressing=?,is_use=? where classid="+classid+" and classroom=room_name";
        return executeUpdate(sql2,progressing,is_use);
    }

    @Override
    public List<Map> selectClassRoom(String roomName) {
        String sql="select * from class_room where room_name=? and del_flag=0";
        return executeQuery(sql,roomName);
    }


    @Override
    public int deleteClassRoom(String roomName) {
        String sql="update class_room set del_flag=1 where room_name=?";
        return executeUpdate(sql,roomName);
    }
    @Override
    public List<Map> query_course() {
        String sql="select*from course where del_flag=0 and state=0";
        return this.executeQuery(sql);
    }

    @Override
    public int addClassRoom(String roomName) {
        String sql="insert into class_room (room_name,del_flag,is_use) values (?,0,0)";
        return this.executeUpdate(sql,roomName);
    }

    @Override
    public int updateClassRoomIsUse(String room_name) {
        String sql="update class_room set is_use=1 where room_name=?";
        return this.executeUpdate(sql,room_name);
    }


    public int updateClassRoomIsNotUse(String room_name) {
        String sql="update class_room set is_use=0 where room_name=?";
        return this.executeUpdate(sql,room_name);
    }

    @Override
    public List<Map> queryOneClass(int classid) {
        String sql="select course_name,classid,courseid,classname,class.del_flag,progressing,limitnum,acnumber,opentime,endtime,classroom,class.teacher,remark,dept_name from class,course where class.del_flag=0 and class.courseid=course.course_id and classid=?";
        return this.executeQuery(sql,classid);
    }

    @Override
    public int updateOneClass(List list) {
        String sql2="update class,class_room set courseid=?,classname=?,limitnum=?,opentime=?,endtime=?,classroom=?,teacher=?,dept_name=?,is_use=1 where classid=? and room_name=?";
       // String sql="update class set courseid=?,classname=?,limitnum=?,opentime=?,endtime=?,classroom=?,teacher=?,dept_name=? where classid=?";
        return executeUpdate(sql2,list.get(0),list.get(1),list.get(2),list.get(3),list.get(4),list.get(5),list.get(6),list.get(7),list.get(8),list.get(5));
    }

    @Override
    public int delete_class(int classid) {
        String sql="update class set del_flag=1 where classid=?";
        return this.executeUpdate(sql,classid);
    }
}
